package com.teach.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class RedisUtils {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisTemplate<Object, Object> redisTemplate;


    public void setStr(String key,Object obj){
        redisTemplate.opsForValue().set(key,obj);
    }

    /**
     * 删除指定key
     * @param key
     */
    public void del(String key){
        stringRedisTemplate.delete(key);
    }

    /**
     * 删除Obj缓存
     * @param o
     */
    public void delObj(Object o){
        redisTemplate.delete(o);
    }

    public void setList(String key, List list){
        redisTemplate.opsForValue().set(key,list);
    }
    public void setList(String key, List list,long times){
        redisTemplate.opsForValue().set(key,list,times, TimeUnit.SECONDS);
    }
    public List getList(String key){
        return (List) redisTemplate.opsForValue().get(key);
    }

    public void hset(String hkey,String key,Object obj){
        redisTemplate.opsForHash().put(hkey,key,obj);
    }

    public Object hget(String hkey,String key){
        return redisTemplate.opsForHash().get(hkey,key);
    }

    public void hdel(String hkey,String key){
        redisTemplate.opsForHash().delete(hkey,key);
    }


    /**************************************************************/
    public void setBoundHash(String hkey,Object key,Object obj){
        redisTemplate.boundHashOps(hkey).put(key,obj);
    }

    public Object getBoundHash(String hkey,Object key){
        return redisTemplate.boundHashOps(hkey).get(key);
    }

    public List<Object> getBoundHashList(String hkey){
        return redisTemplate.boundHashOps(hkey).values();
    }

    public void setBoundListShuffle(String key,List list){
        redisTemplate.boundListOps(key).leftPushAll(list.toArray(new Integer[16000]));
    }

    public Object rightPop(String key){
        Object o = redisTemplate.boundListOps(key).rightPop();
        return o;
    }

    public void hIncream(String hkey, String key) {
        Integer count= (Integer) redisTemplate.opsForHash().get(hkey, key);
        if(count==null){
            count = 0;
        }
        redisTemplate.opsForHash().put(hkey,key,count+1);
    }

}